<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of LangSelector
 *
 * @author vampired
 */
class Golib_Controller_Plugin_LangSelector extends Zend_Controller_Plugin_Abstract {

    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        if($this->_request->getModuleName()=='admin') return;
        $lang = $request->getParam('lang', '');

        if ($lang !== 'en' && $lang !== 'ar')
            $request->setParam('lang', 'en');

        $lang = $request->getParam('lang');
        if ($lang == 'en')
            $locale = 'en_CA';
        else
            $locale = 'ar_EG';

        $zl = new Zend_Locale();
        $zl->setLocale($locale);
        Zend_Registry::set('Zend_Locale', $zl);
        $dictionary = $this->prepareArray($lang);
//        var_dump($dictionary);die();
        $translate = new Zend_Translate('array', $dictionary, $lang);
        Zend_Registry::set('Zend_Translate', $translate);
    }

    private function prepareArray($lang) {
        $dictionary = unserialize(file_get_contents(ROOT_PATH . '/data/lang/menu-' . $lang));
            $controller = $this->_request->getControllerName();
            if ($controller == 'index') {
                $controller = 'home';
            }
            if (file_exists(ROOT_PATH . '/data/lang/' . $controller . '-' . $lang)) {
                $dictionary+=unserialize(file_get_contents(ROOT_PATH . '/data/lang/' . $controller . '-' . $lang));
            }
//        }else{
//            if($this->_request->getControllerName()=='index'){
//                $dictionary=array('en'=>array(),'ar'=>array());
//                    $dictionary['en']['home']=unserialize(file_get_contents(ROOT_PATH . '/data/lang/home-en'));
//                    $dictionary['en']['about']=unserialize(file_get_contents(ROOT_PATH . '/data/lang/about-en'));
//                    $dictionary['en']['contact']=unserialize(file_get_contents(ROOT_PATH . '/data/lang/contact-en'));
//                    $dictionary['ar']['home']=unserialize(file_get_contents(ROOT_PATH . '/data/lang/home-ar'));
//                    $dictionary['ar']['about']=unserialize(file_get_contents(ROOT_PATH . '/data/lang/about-ar'));
//                    $dictionary['ar']['contact']=unserialize(file_get_contents(ROOT_PATH . '/data/lang/contact-ar'));
//            }
//        }
//        var_dump($dictionary);
        return $dictionary;
    }

}

?>